import actionTypes from "./actionTypes"
import http from "../../service/http"

export function LoadVarietyAction(payload) {
  return {
    type: actionTypes.LOAD_VARIETY_LIST,
    payload: payload,
  }
}

export function LoadShoppingDataAction(payload) {
  return {
    type: actionTypes.LOAD_SHOPPING_DATA,
    payload: payload,
  }
}

export function LoadVarietyActionAsync(payload) {
  return dispatch => {
    return http.varietyList(payload).then((res) => {
      if (res.code !== 200) return
      let list = []
      res.data.forEach((item) => {
        let check = list.every((item2) => {
          return item2.classId !== item.classId
        })
        check ? list.push(item) : null
      })
      //所有菜品
      let list2 = res.data.map((item, index) => {
        item.varietyId = item.id
        item.id = index
        item.num = 0
        return item
      })
      //分类后的菜品
      list = list.map((item, index) => {
        let listChild = []
        list2.forEach((item2) => {
          if (item.classId === item2.classId) {
            listChild.push(item2)
          }
        })
        item.listChild = listChild
        item.key = index
        item.number = 0
        return item
      })
      dispatch(LoadVarietyAction(list))
      dispatch(LoadShoppingDataAction({ list2: list2, list: list }))
    })
  }
}
// export function UpdateVarietyActionAsync(payload) {
//   return dispatch => {
//     return http.varietyUpdate(payload).then((res) => {
//       if (res.code !== 200) return
//     })
//   }
// }
// export function AddVarietyActionAsync(payload){
//   return dispatch=>{
//     return http.varietyAdd(payload)
//   }
// }